﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Autodesk.Connectivity.WebServices;
using Autodesk.Connectivity.WebServicesTools;

namespace EffectiveFolderPermissions
{
    public partial class SettingsForm : Form
    {
        public Settings Settings { get; private set;}

        private WebServiceManager webServiceManager; 


        public SettingsForm(Settings settings, WebServiceManager wsm)
        {
            InitializeComponent();

            webServiceManager = wsm;
            this.Settings = settings;
            m_showInactiveUsersCheckBox.Checked = Settings.ShowInactiveUsers;
            m_showUsersWoAccessCheckBox.Checked = Settings.ShowUsersWithoutVaultAccess;
            m_depthTextBox.Text = Settings.FolderDepth.ToString();
            SetListBox(m_includeListBox, Settings.IncludedFolders);
            SetListBox(m_excludeListBox, Settings.ExcludedFolders);
        }

        private void SetListBox(ListBox listBox, List<string> strings)
        {
            listBox.Items.Clear();
            if (strings == null)
                 return;

            foreach (string s in strings)
            {
                listBox.Items.Add(s);
            }
        }

        private List<string> ReadListBox(ListBox listBox)
        {
            List<string> retval = new List<string>();
            foreach (object o in listBox.Items)
            {
                retval.Add(o.ToString());
            }

            return retval;
        }

        private void m_cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void m_okButton_Click(object sender, EventArgs e)
        {
            // save the settings back to the object

            int depth;
            if (!int.TryParse(m_depthTextBox.Text, out depth))
            {
                MessageBox.Show("You must enter a number for the folder depth.");
                return;
            }

            if (depth <= 0)
            {
                MessageBox.Show("Folder depth must be a positive number.");
                return;
            }

            Settings.FolderDepth = depth;
            Settings.ShowInactiveUsers = m_showInactiveUsersCheckBox.Checked;
            Settings.ShowUsersWithoutVaultAccess = m_showUsersWoAccessCheckBox.Checked;
            Settings.IncludedFolders = ReadListBox(m_includeListBox);
            Settings.ExcludedFolders = ReadListBox(m_excludeListBox);

            DialogResult = DialogResult.OK;
        }

        private void m_listBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListBox listbox = sender as ListBox;
            if (listbox != null)
                AddFolderToListbox(listbox);
        }

        private void AddFolderToListbox(ListBox listbox)
        {
            
            FolderBrowseDialog selectFolder = new FolderBrowseDialog(webServiceManager.DocumentService);
            DialogResult result = selectFolder.ShowDialog();
            if (result == DialogResult.OK)
            {
                Folder folder = selectFolder.SelectedFolder;
                listbox.Items.Add(folder.FullName);
            }
        }

        private void m_listBox_KeyDown(object sender, KeyEventArgs e)
        {
            ListBox listbox = sender as ListBox;

            if (sender != null)
            {
                if (e.KeyCode == Keys.Delete)
                    DeleteFromListbox(listbox);
            }

        }

        private void DeleteFromListbox(ListBox listbox)
        {
            listbox.Items.Remove(listbox.SelectedItem);
        }
    }
}
